<?php

namespace app\job\queue;

use app\common\enum\fp\PostAuditEnum;
use app\common\library\aliyuns\MtsClient;

use app\common\model\fp\PostAudit;
use app\common\model\fp\PostMedia;

use app\logic\es\fp\EsPost;
use think\Exception;
use think\facade\Log;
use think\queue\Job;

/**
 * https://github.com/top-think/think-queue/tree/3.0
 */
class PostAuditJob
{

    public function fire(Job $job, $data)
    {
        if ($job->attempts() > 10) {
            //通过这个方法可以检查这个任务已经重试了几次了
            $job->failed($data);
            $job->delete();
            return;
        }

        try {
            if (PostAuditEnum::from($data['status']) === PostAuditEnum::APPROVED) {

                //导入动态到es
                EsPost::getInstance()->import($data);

                $job->delete();
            }
        } catch (\Throwable $e) {
            Log::channel('job')->error($e->getMessage() . PHP_EOL . $e->getTraceAsString());
            throw $e;
        }


        // 也可以重新发布这个任务
        // $delay = 1;
        //$job->release($delay); //$delay为延迟时间

    }

    public function failed($data)
    {
        // ...任务达到最大重试次数后，失败了
        custom_log(json_encode($data, 256), null, 'failed_job');
    }

}
